<template>
  <a-modal :title="title" :visible="open" :confirm-loading="submitLoading" @ok="handleSubmit" @cancel="handleClose">
    <a-form-model ref="formRef" :model="form" :rules="rules" :labelCol="{span: 4}" :wrapperCol="{span: 20}">
      <a-form-model-item label="字典类型" prop="dictType">
        <a-radio-group :default-value="form.dictType" v-model="form.dictType" button-style="solid">
          <a-radio-button :value="1">系统</a-radio-button>
          <a-radio-button :value="2">业务</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="字典名称" prop="dictName">
        <a-input v-model="form.dictName" placeholder="请输入字典名称" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="字典编码" prop="dictCode">
        <a-input v-model="form.dictCode" placeholder="请输入字典编码" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="描述" prop="description">
        <a-input v-model="form.description" placeholder="请输入描述" type="textarea" allow-clear/>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import {getSysDict, saveSysDict, updateSysDict} from '@/api/system/sysDict'

export default {
  data() {
    return {
      submitLoading: false,
      title: '',
      open: false,
      // 表单
      form: {},
      rules: {
        dictType: [{required: true, trigger: ['blur', 'change'], message: '请选择字典类型'}],
        dictName: [{required: true, trigger: ['blur', 'change'], message: '请输入字典名称'}],
        dictCode: [{required: true, trigger: ['blur', 'change'], message: '请输入字典编码'}],
        description: [{required: true, trigger: ['blur', 'change'], message: '请输入描述'}]
      }
    }
  },
  methods: {
    /** 点击关闭 */
    handleClose() {
      this.reset()
      this.open = false
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        dictType: 1,
        dictName: undefined,
        dictCode: undefined,
        description: undefined
      }
    },
    /** 点击新增 */
    handleAdd() {
      this.reset()
      this.title = '添加字典'
      this.open = true
    },
    /** 点击修改 */
    handleUpdate(row) {
      this.reset()
      getSysDict(row.id).then(res => {
        this.title = '修改字典'
        this.open = true
        this.$nextTick(() => {
          this.form = res.data
        })
      })
    },
    /** 点击保存 */
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id && this.form.id > 0) {
            updateSysDict(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            saveSysDict(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        }
      })
    }
  }
}
</script>
